//
//  StatusRetweetView.swift
//  Weibo
//
//  Created by 孙成宇 on 15/12/2.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class StatusRetweetView: UIView {

private var bottomconstraint: Constraint?

    var retweetedStatus: Status?{
        didSet {

            print(retweetedStatus?.user?.name)
            retweetedLabel.text = "@" + (retweetedStatus?.user?.name ?? "") + ":" + (retweetedStatus?.text ?? "")
            // 使用约束属性  记录可能产生复用的约束  将可能复用的约束 解除掉
            bottomconstraint?.uninstall()

            //设置配图视图的图片数组
            //智能提示不好使
            if let urls = retweetedStatus?.imageURLs where urls.count != 0 {
                //有配图视图
                //1.设置数据源
                pictureView.imageURLs = urls
                //2.更改视图的底部约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomconstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图视图  更改底部约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //记录底部约束
                    //1.对底部添加约束  2.转换成约束对象 并且使用属性记录
                    self.bottomconstraint =  make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
            }
        }




        }







    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor  = UIColor(white: 0.8, alpha: 1)
           setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private func setupUI() {

        addSubview(pictureView)
        addSubview(retweetedLabel)


   retweetedLabel.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(self.snp_left).offset(StatusCellMargin)

    make.top.equalTo(self.snp_top).offset(StatusCellMargin)


        }
     pictureView.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(retweetedLabel.snp_left)

    make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)


        }

     self.snp_makeConstraints { (make) -> Void in
      self.bottomconstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

    }




    private lazy var retweetedLabel: UILabel = UILabel(title: "转发微博", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMargin)

    private lazy var pictureView: StatusPictureView = StatusPictureView()




}
